<?php

/**
 * Portal PSG
 * 
 * Pomocnik zapytań asynchronicznych AJAX
 */
require_once '../_boot.php'; // import biblioteki rozruchowej

RunApplication(APP_DOMAIN, APP_URLPATH, APP_PORT);

import('Application.Config');
import('Psg.Auth');
import('Psg.Notifier');
import('Psg.Teryt');

switch ($_GET['action']) {

    /**
     * Wysyła formularz rejestracji
     */
    case 'sendRegistrationForm':
        $nick = $_GET['nick'];
        $email = $_GET['email'];
        $city = $_GET['city'];
        $state = $_GET['state'];
        if (!\Psg\Auth::registrationExist(NULL, $nick) && !\Psg\Auth::userNickExists($nick)) {
            if (!\Psg\Auth::userExists($_GET['email']) && !\Psg\Auth::registrationExist($_GET['email'])) {
                $dateStamp = date('Y-m-d');
                \Psg\Auth::sendRegistration($email, $nick, $state, $city, $dateStamp);
                echo 1;
            } else {
                echo 0;
            }
        } else {
            echo 0;
        }
        break;

    /**
     * Zwraca AJAX-loadera (GIF)
     */
    case 'getAjaxLoader':
        $loaderUrl = Url::base() . 'res/ajaxloader.gif';
        echo "<img src=\"{$loaderUrl}\" alt=\"\"/>";
        break;

    /**
     * Waliduje Nick
     */
    case 'validateNick':
        $nick = $_GET['nick'];
        $syntax = preg_match('/^[a-zA-Z0-9\_]{2,15}$/', $_GET['nick']);
        if ($syntax && !\Psg\Auth::registrationExist(NULL, $nick) && !\Psg\Auth::userNickExists($nick)) {
            echo 1;
        } else {
            echo 0;
        }
        break;

    /**
     * Waliduje Email
     */
    case 'validateEmail':
        $mailSyntax = filter_var($_GET['email'], FILTER_VALIDATE_EMAIL);
        $checkUsrNotExists = !\Psg\Auth::userExists($_GET['email']) && !\Psg\Auth::registrationExist($_GET['email']);
        echo ($mailSyntax && $checkUsrNotExists) ? '1' : '0';
        break;
    /**
     *  sprawdza czy istnieje uzytkownik o danym adresie email
     */
    case 'userExists':
        if (\Psg\Auth::userExists($_GET['email'])&&!\Psg\Auth::isBlockedUser($_GET['email']))
            echo 1;
        else
            echo 0;
        break;

    /**
     *  wysyla zadanie email do logowania
     */
    case 'sendLoginRequest':
        $ret = \Psg\Auth::sendLoginReqToken($_GET['email']);
        if (is_string($ret)) {
            if (!isset($_SESSION['sendedToken'])) {
                $_SESSION['sendedToken'] = 1;
                $N = new \Psg\Notifier();
                $N->Send([$_GET['email']], 'Żądanie logowania w Portalu PSG', View::factory('_mail_loginRequest')
                                ->set('to', $_GET['email'])
                                ->set('token', $ret)
                );
            }
            echo '1';
        } else {
            echo 0;
        }
        break;

    /**
     * Zwraca liste rozwijana Wojewodztw
     */
    case 'getStatesForm':
        $return = "<select id=\"{$_GET['name']}\"><option value=99>---</option>";
        foreach (\Psg\Teryt::getStates() as $rowid => $rowcol) {
            $return.="<option value=\"{$rowcol['id']}\">{$rowcol['nazwa']}</option>";
        }
        echo $return . '</select>';
        break;

    /**
     * Zwraca liste rozwijana Miast danego województwa
     */
    case 'getCitiesFromIdForm':
        if ($_GET ['id'] != 99) {
            $return = "<select id=\"{$_GET['name']}\">";
            foreach (\Psg\Teryt::getCityOfStateId($_GET['id']) as $rowid => $rowcol) {
                $return .= "<option value=\"{$rowcol['id']}\">{$rowcol['nazwa']}</option>";
            }
            echo $return . '</select>';
        }
        break;

    /**
     * domyslna akcja
     */
    default:
        Url::redirect();
        break;
}